import QtQuick 1.0
import com.meego 1.0
import QtMultimediaKit 1.1

Page {
    id: container

    tools: ToolBarLayout {
            id: toolBarLayout

            ToolButton {
                flat: false
                visible: true
                width: 100
                height: 50
                text: "Chiudi"
                onClicked: Qt.quit()
            }

            ToolButton {
                id: toolVerificaAuto
                visible: true
                text: "Verifica"
                flat: false
                height: 50
                onClicked: {
                    clickSound.play()
                    if (objPlate.text === "" || objPlate.text === "undefined")
                        return;
                    startTheftCheck(objPlate.text)
                }
            }
        }


    function startTheftCheck(plate) {
        waitDialog.running = true;
        waitDialog.visible = true;
        checkPolice.on_cmdSend_clicked(plate);
        toolVerificaAuto.enabled = false;
        //toolClear.enabled = false;
    }

    function searchCompleted(arrayInfo) {
        toolVerificaAuto.enabled = true;
        waitDialog.running = false;
        waitDialog.visible = false;

        var displayForm = Qt.createComponent("SearchedInfo.qml");
        var obj = null;

        if (displayForm.status === Component.Ready) {
            obj = displayForm.createObject(pageStack);
            if (obj !== null) {
                obj.populateInfo(arrayInfo);
                pageStack.push(obj);
            }
        }
    }

    function noTheft() {
        toolVerificaAuto.enabled = true;
        waitDialog.running = false;
        waitDialog.visible = false;
        dialog.z = 100;
        dialog.opacity = 1;
    }

    Connections {
        target: checkPolice
        onSearchCompleted: searchCompleted(info)
        onVehicleValid: noTheft()
    }

    SoundEffect {
        id: clickSound
        source: "../../Audio/click.wav"
    }

    Text {
        id: labelTitle
        y: container.y + 15
        color: "#be0808"
        width: container.width
        horizontalAlignment: Text.AlignHCenter
        text: "Ricerca online veicolo"
        style: Text.Outline
        font.pointSize: 22
    }

    Text {
        id: labelPlate
        color: "#5574b7"
        anchors.left: parent.left
        anchors.leftMargin: 15
        y: 120
        text: "Targa veicolo da ricercare:"
        font.bold: true
        font.pointSize: 15
    }

    Text {
        id: infoMinistero
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom; leftMargin: 15; rightMargin: 15; bottomMargin: 40 }
        wrapMode: "WordWrap"
        text: "Informazioni recuperate dal Ministero del lavoro: le notizie relative ad auto, motoveicoli e targhe rubate hanno valore puramente informativo e di nessun valore legale" +
                " in quanto non coincidono con quelle inserite negli archivi operativi della Banca Dati Interforze poiche' aggiornate con un ritardo temporale di diversi giorni."
        horizontalAlignment: Text.AlignHCenter
        font.pointSize: 18

    }

    TextField {
        id: objPlate
        y: labelPlate.height + labelPlate.y + 10
        width: container.width - 30
        anchors.left: parent.left
        anchors.leftMargin: 15
        placeholderText: "Inserire la targa di un veicolo"
        clip: true
        smooth: true
    }



    DialogMessage {
        id: dialog
        setMessage: "Il veicolo ricercato online non risulta rubato!"
        anchors.centerIn: container
        //x: (container.width - container.width) / 2
        //y: (container.height - container.height) / 2
        opacity: 0
    }

    WaitSearching {
        id: waitDialog
        width: 150
        height: 90
        x: (parent.width - waitDialog.width) / 2
        y: (parent.height - waitDialog.height) / 2
        z: 2
    }
}
